<template>
  <div>
    <div class="pageheader">
      <XHeader :left-options="{backText:''}" :title="xheader.title" class="XHeader"></XHeader>
    </div>
    <div class="pagetop">
      <div class="toptab">
        <div
          v-for="(item,index) in tab"
          class="item"
          :key="index"
          :class="tabIndex==index?'active':''"
          @click="onTabTap(index, item)"
        >
          <div class="time">
            <!--            {{(new Date(item.seckill_starttime.replace(/-/g,'/')))}}-->
            <!--            {{(new Date(item.seckill_starttime).replace(/-/g,'/')).getHours()}}-->
            {{`${(new Date(item.seckill_starttime.replace(/-/g,'/'))).getHours()}:${(new
            Date(item.seckill_starttime.replace(/-/g,'/'))).getMinutes()}`}}
          </div>
          <div class="type">{{item.seckill_status==0?'即将开始':item.seckill_status==1?'秒杀中':'已结束'}}</div>
        </div>
      </div>
    </div>
    <div class="page">
      <div style="margin-top: 12px;">
        <div v-for="(item,index) in items" :key="index" class="block">
          <div class="seckillitem" @click="onInfo(item)">
            <chimage class="img" :src="item.goods_info.goods_image_main"></chimage>
            <div class="content">
              <div class="title">{{item.goods_info.goods_name}}</div>
              <div class="operation">
                <div class="price">￥{{item.seckill_price}}</div>
                <button type="warn">{{status==0?'即将开始':status==1?'立即抢购':'已结束'}}</button>
                <!--                  <button type="warn">{{'立即抢购'}}</button>-->
              </div>
              <div class="other">
                <div class="d-price">￥{{item.goods_info.reference_price}}</div>
                <div class="amount">已售{{soldPercentage(item)}}</div>
                <div class="progressbar">
                  <div class="progress" :style="{width:soldPercentage(item)}"></div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import Mall from '../../../dll/mall'
  // import moment from 'moment'

  export default {
    name: 'seckill',
    data: () => {
      return {
        xheader: {
          title: '秒杀'
        },
        tab: [],
        tabIndex: 0,
        items: [],
        status: 0
      }
    },
    created () {
      this.getSeckillTime()
    },
    computed: {
      // 已售百分比
      soldPercentage () {
        return (item) => {
          return `${((item.seckill_number - item.seckill_number_remain) / item.seckill_number) * 100}%`
        }
      }
    },
    methods: {
      onTabTap (index, item) {
        this.tabIndex = index
        this.items = item.seckill_goods_list
        this.status = item.seckill_status
      },
      onInfo (item) {
        if (item.status == 1) {
          this.$router.push({
            name: 'mallSeckillInfo',
            params: { id: item.seckill_id }
          })
        } else {
          this.$router.push({ name: 'mallInfo', params: { id: item.goods_info.goods_sku } })
        }
      },
      // 获取秒杀列表
      async getSeckillTime () {
        this.$vux.loading.show({
          text: '加载中'
        })
        let res = await Mall.getNewSeckill()
        this.$vux.loading.hide()
        if (res.code == 0) {
          let list = []
          if (res.data.seckill_list.length) {
            list = res.data.seckill_list.filter((item) => { // 过滤掉已经过期的
              return item.seckill_status != 2
            })
          }
          if (!res.data.seckill_list.length || !list.length) {
            this.$vux.confirm.show({
              title: '提示',
              content: '秒杀尚未开始，请稍后再来。',
              showCancelButton: false,
              onConfirm: () => {
                this.$router.back()
              }
            })
            return
          }

          list = list.sort((x, y) => {
            return y.seckill_status - x.seckill_status
          })
          this.tab = list
          this.items = list[0].seckill_goods_list
          this.status = list[0].seckill_status
        } else {
          this.$vux.confirm.show({
            title: '提示',
            content: res.msg,
            showCancelButton: false,
            onConfirm: () => {
              this.$router.back()
            }
          })
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  .toptab {
    border-top: 1px solid #fbf8f7;
    box-sizing: border-box;
    width: 100%;
    display: flex;
    height: 64px;
    flex-wrap: nowrap;
    overflow: auto;
    background-color: #fff;

    .item {
      height: 100%;
      width: 75px;
      min-width: 75px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      > .time {
        line-height: 24px;
        font-size: 17px;
        color: #d1c8c6;
      }

      > .type {
        margin-top: 4px;
        font-size: 13px;
        color: #d1c8c6;
        line-height: 18px;
      }
    }

    .item.active {
      background-color: #eca13e;

      > .time,
      > .type {
        color: #fff;
      }
    }
  }

  .seckillitem {
    padding: 24px 16px;
    box-sizing: border-box;
    background-color: #fff;
    display: flex;
    align-items: center;

    > .img {
      display: block;
      width: 90px;
      height: 90px;
      margin-right: 24px;
      background-color: #fbf8f7;
    }

    > .content {
      flex: 1;
    }

    .title {
      font-size: 14px;
      color: #9f8d89;
      line-height: 20px;
      height: 40px;
      text-overflow: ellipsis;
      overflow: hidden;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }

    .operation {
      margin-top: 22px;
      display: flex;
      align-items: center;
    }

    .price {
      flex: 1;
      font-size: 18px;
      color: #b02910;
      line-height: 25px;
    }

    button {
      width: auto;
      height: 28px;
      line-height: 28px;
      font-size: 12px;
      padding: 0 4px;
    }

    .other {
      margin-top: 15px;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }

    .d-price {
      font-size: 12px;
      color: #d3cbc9;
      line-height: 17px;
      text-decoration: line-through;
    }

    .progressbar {
      width: 64px;
      height: 12px;
      background-color: #efded9;
      border-radius: 6px;
      position: relative;
    }

    .progress {
      background-color: #b02910;
      height: 12px;
      border-radius: 6px;
      top: 0;
      left: 0;
    }

    .amount {
      font-size: 12px;
      color: #d3cbc9;
      line-height: 17px;
    }
  }

  .page {
    padding-top: 64px;
  }

  .pagetop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 999;
  }
</style>
